import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useStudentStore = defineStore('student', () => {
  const studentInfo = ref({})
  const registrationStatus = ref({})
  const paymentInfo = ref([])
  const notices = ref([])
  const schedule = ref([])
  const loading = ref(false)

  // 获取学生信息
  const fetchStudentInfo = async () => {
    loading.value = true
    try {
      // 模拟API调用
      const mockData = {
        id: 1,
        studentId: '2024001',
        name: '张三',
        gender: '男',
        idCard: '123456789012345678',
        phone: '13800138000',
        email: 'zhangsan@example.com',
        major: '计算机科学与技术',
        class: '计科2024-1班',
        dormitory: {
          building: '1号楼',
          room: '101',
          bed: 'A'
        },
        emergencyContact: {
          name: '张父',
          phone: '13900139000',
          relationship: '父亲'
        }
      }
      
      studentInfo.value = mockData
      return { success: true, data: mockData }
    } catch (error) {
      return { success: false, message: error.message }
    } finally {
      loading.value = false
    }
  }

  // 获取报到状态
  const fetchRegistrationStatus = async () => {
    try {
      const mockStatus = {
        overall: 75, // 总进度百分比
        steps: [
          { name: '信息确认', status: 'completed', description: '已完成个人信息确认' },
          { name: '缴费', status: 'completed', description: '已完成学费缴纳' },
          { name: '宿舍分配', status: 'in_progress', description: '宿舍分配中' },
          { name: '报到确认', status: 'pending', description: '等待现场报到' }
        ]
      }
      
      registrationStatus.value = mockStatus
      return { success: true, data: mockStatus }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 获取缴费信息
  const fetchPaymentInfo = async () => {
    try {
      const mockPayments = [
        {
          id: 1,
          name: '学费',
          amount: 5000,
          status: 'paid',
          dueDate: '2024-08-31',
          paidDate: '2024-08-15',
          paymentMethod: '支付宝'
        },
        {
          id: 2,
          name: '住宿费',
          amount: 1200,
          status: 'paid',
          dueDate: '2024-08-31',
          paidDate: '2024-08-15',
          paymentMethod: '微信支付'
        },
        {
          id: 3,
          name: '教材费',
          amount: 300,
          status: 'unpaid',
          dueDate: '2024-09-15'
        }
      ]
      
      paymentInfo.value = mockPayments
      return { success: true, data: mockPayments }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 获取通知公告
  const fetchNotices = async () => {
    try {
      const mockNotices = [
        {
          id: 1,
          title: '2024年新生报到须知',
          content: '请各位新生按时报到...',
          publishDate: '2024-08-01',
          important: true
        },
        {
          id: 2,
          title: '宿舍分配通知',
          content: '宿舍分配结果已公布...',
          publishDate: '2024-08-10',
          important: false
        }
      ]
      
      notices.value = mockNotices
      return { success: true, data: mockNotices }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 获取日程安排
  const fetchSchedule = async () => {
    try {
      const mockSchedule = [
        {
          id: 1,
          title: '新生报到',
          date: '2024-09-01',
          time: '08:00-17:00',
          location: '体育馆',
          type: 'important'
        },
        {
          id: 2,
          title: '入学教育',
          date: '2024-09-02',
          time: '09:00-11:00',
          location: '大礼堂',
          type: 'normal'
        }
      ]
      
      schedule.value = mockSchedule
      return { success: true, data: mockSchedule }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 提交信息
  const submitInfo = async (data) => {
    loading.value = true
    try {
      // 模拟API调用
      studentInfo.value = { ...studentInfo.value, ...data }
      return { success: true, message: '信息提交成功' }
    } catch (error) {
      return { success: false, message: error.message }
    } finally {
      loading.value = false
    }
  }

  // 在线缴费
  const makePayment = async (paymentId, paymentMethod) => {
    loading.value = true
    try {
      // 模拟API调用
      const paymentIndex = paymentInfo.value.findIndex(p => p.id === paymentId)
      if (paymentIndex !== -1) {
        paymentInfo.value[paymentIndex].status = 'paid'
        paymentInfo.value[paymentIndex].paidDate = new Date().toISOString().split('T')[0]
        paymentInfo.value[paymentIndex].paymentMethod = paymentMethod
      }
      return { success: true, message: '缴费成功' }
    } catch (error) {
      return { success: false, message: error.message }
    } finally {
      loading.value = false
    }
  }

  return {
    studentInfo,
    registrationStatus,
    paymentInfo,
    notices,
    schedule,
    loading,
    fetchStudentInfo,
    fetchRegistrationStatus,
    fetchPaymentInfo,
    fetchNotices,
    fetchSchedule,
    submitInfo,
    makePayment
  }
})